{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:04.773413Z",
     "start_time": "2025-10-17T08:25:04.583202Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from pandapower.plotting import simple_plot\n",
    "from pandapower.test.shortcircuit.test_1ph import vde_232"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create example net from IEC 60909:4-2021 Figure 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.011332Z",
     "start_time": "2025-10-17T08:25:04.781005Z"
    }
   },
   "outputs": [],
   "source": [
    "net = vde_232()\n",
    "vn_trafo_hv = net.trafo.vn_hv_kv\n",
    "vn_trafo_lv = net.trafo.vn_lv_kv\n",
    "sn_mva = net.trafo.sn_mva\n",
    "vk0_percent = net.trafo.vk0_percent\n",
    "vkr0_percent = net.trafo.vkr0_percent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.134241Z",
     "start_time": "2025-10-17T08:25:05.019791Z"
    }
   },
   "outputs": [],
   "source": [
    "simple_plot(net, plot_gens=True, gen_size=0.05, trafo_size=10, ext_grid_size=5);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the zero-sequence impedance of the power station unit transformer related to the HV side (without correction factor $K_T$)\n",
    "\n",
    "$\\underline{Z}_{(0)THV} = (\\frac{u_{Rr}}{100 \\%} + j \\frac{u_{Xr}}{100 \\%}) \\frac{U_{rTHV}^2}{S_{rT}}$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.162544Z",
     "start_time": "2025-10-17T08:25:05.146428Z"
    }
   },
   "outputs": [],
   "source": [
    "vkx0_percent = np.sqrt(np.square(vk0_percent) - np.square(vkr0_percent))\n",
    "z_0THV = (vkr0_percent / 100 + 1j * vkx0_percent / 100) * (np.square(vn_trafo_hv) / sn_mva)\n",
    "z_0THV.at[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the correction factor for the power system unit with OLTC \n",
    "\n",
    "$K_S = \\frac{U_{nQ}^2}{U_{rG}^2} \\cdot \\frac{U_{rTLV}^2}{U_{rTHV}^2} \\cdot \\frac{c_{max}}{1+|x_d^{''} - x_T| \\sqrt{1-cos^2_{\\phi rG}}}$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.220722Z",
     "start_time": "2025-10-17T08:25:05.197578Z"
    }
   },
   "outputs": [],
   "source": [
    "v_g = net.gen.vn_kv\n",
    "v_q = net.bus.loc[net.trafo[\"hv_bus\"].values, \"vn_kv\"].values\n",
    "ps_cmax = 1.1\n",
    "x_t = net.trafo[\"vk_percent\"].values / 100\n",
    "xdss_pu = net.gen.xdss_pu\n",
    "sin_phi_gen = np.sqrt(np.clip(1 - net.gen.cos_phi.values**2, 0, None))\n",
    "\n",
    "ks = (v_q**2/v_g**2) * (vn_trafo_lv**2/vn_trafo_hv**2) * ps_cmax / (1 + np.abs(xdss_pu - x_t) * sin_phi_gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.252052Z",
     "start_time": "2025-10-17T08:25:05.230030Z"
    }
   },
   "outputs": [],
   "source": [
    "ks.at[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the corrected zero-sequence impedance for the power system unit with OLTC \n",
    "\n",
    "$\\underline{Z}_{(0)SK} = K_S \\cdot \\underline{Z}_{(0)THV} + 3 \\cdot \\underline{Z}_N$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.297794Z",
     "start_time": "2025-10-17T08:25:05.279408Z"
    }
   },
   "outputs": [],
   "source": [
    "zn = 1j * net.trafo.xn_ohm\n",
    "z_0SK = ks * z_0THV + 3*zn\n",
    "z_0SK.at[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the correction factor for the power system unit without OLTC \n",
    "\n",
    "$K_{SO} = \\frac{U_{nQ}}{U_{rG} \\cdot (1+p_G)} \\cdot \\frac{U_{rTLV}}{U_{rTHV}} \\cdot (1 \\pm p_T) \\cdot \\frac{c_{max}}{1+x_d^{''} \\sqrt{1-cos^2_{\\phi rG}}}$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.339124Z",
     "start_time": "2025-10-17T08:25:05.315886Z"
    }
   },
   "outputs": [],
   "source": [
    "p_g = net.gen.pg_percent / 100\n",
    "p_t = net.trafo['pt_percent'] / 100\n",
    "kso = (v_q / (v_g * (1 + p_g))) * (vn_trafo_lv / vn_trafo_hv) * (1 - p_t) * ps_cmax / (1 + xdss_pu * sin_phi_gen)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the corrected zero-sequence impedance for the power system unit without OLTC \n",
    "\n",
    "$\\underline{Z}_{(0)SOK} = K_{SO} \\cdot \\underline{Z}_{(0)THV} + 3 \\cdot \\underline{Z}_N$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T08:25:05.370086Z",
     "start_time": "2025-10-17T08:25:05.348272Z"
    }
   },
   "outputs": [],
   "source": [
    "z_0SOK = kso * z_0THV + 3*zn\n",
    "z_0SOK.at[0]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
